<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"
	import="java.util.*,com.telecom.cms.dao.*,com.telecom.cms.dto.*"%>
<%@page import="java.sql.*"%>
<%@page import="com.telecom.cms.dbconnect.*"%>
<%@ taglib uri="/WEB-INF/tree.tld" prefix="tree"%>

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>结果字段选取</title>
		<STYLE type=text/css></STYLE>
		<script type="text/javascript" src="js/dtree.js"></script>
		<script type="text/javascript" src="js/default.js"></script>
		<link rel="stylesheet" type="text/css" href="images/main.css">
		<SCRIPT type="text/javascript">
/************************************************************
此下定义了选项option对象和他的数组
有2个属性text--文字 value--值
Author:王新强
************************************************************/
function Atomoption (text,value){
	this.text=text;
	this.value=value;
}

var atoms=new Array();

/************************************************************
将选定的组下的结果原子加入的选择列表中
Author:王新强
************************************************************/
function ChangeGroup()
{ 
	var i,iGroupIndex;
	iGroupIndex=document.all.groups.selectedIndex;
	iAtomCount=0; 
	while (atoms[iGroupIndex][iAtomCount]!=null) 
	 iAtomCount++;
	 //计算选定组下的原子个数 
	document.all.SrcSelect.length=0;//清空选项数 
	var obj=document.all.ObjSelect;
	var objCount=obj.length;
	for(i=0;i<=iAtomCount-1;i++){//改变下拉菜单的内容 
		var flag=0;
		for(j=0;j<=objCount-1;j++){
			if(obj.options[j].value==atoms[iGroupIndex][i].value){
				flag++;
			}
		}
		if(flag==0)
		document.all.SrcSelect.add(new Option(atoms[iGroupIndex][i].text,atoms[iGroupIndex][i].value));
	}
	document.all.SrcSelect.focus();
}

/************************************************************
上下左右移动option对象.来完成选定字段原子与顺序
Author:王新强
************************************************************/
function moveUp()  
{ 
var theObj=document.all.ObjSelect;
for(var i=1;i<theObj.length;i++)
 {
  if( theObj.options[i].selected && !theObj.options[i-1].selected )
  {
   theObj.options[i].swapNode(theObj.options[i-1]);//与上面一个交换
  }
 }
}
  
function moveDown()
{ 
var theObj=document.all.ObjSelect;
for(var i=theObj.length-2;i>-1;i--)
 {
  if( theObj.options[i].selected && !theObj.options[i+1].selected )
  {
   theObj.options[i].swapNode(theObj.options[i+1]);
  }
 }
}
  
  
function moveLeftOrRight(fromObj,toObj)
{
 var lengthOfToObj=toObj.length;
 for(var i=fromObj.length-1;i>-1;i--)
 {
  if(fromObj.options[i].selected)
  {
   toObj.add(new Option(fromObj.options[i].text,fromObj.options[i].value),lengthOfToObj);//new option(text,value,true,true)
   toObj.options[lengthOfToObj].selected=true;
   fromObj.options[i].removeNode(true);
  }
 }
}

/************************************************************
提交
Author:王新强
************************************************************/
function submitchange(){
	obj=document.changeOutput.ObjSelect;
	if(obj.length<1){
			alert("请选择结果字段!");
			return;
		}
	for(i=0;i<obj.length;i++){
		obj.options[i].selected=true;
	}
	document.changeOutput.target="mainFrame";
	document.changeOutput.submit();
}

/************************************************************
选择模板
Author:王新强
************************************************************/
function querymodel(){
	document.changeOutput.action="atomGet4RSModelDEF.do"
	document.getElementById("operation").value="rslist";
	document.changeOutput.target="mainFrame";
	document.changeOutput.submit();
	window.parent.topFrame.window.location.reload();
}

/************************************************************
选择复制条件
Author:王新强
************************************************************/		
function querycondition(){
	document.changeOutput.action="atomGet4RSModelDEF.do"
	document.getElementById("operation").value="conditionchange";
	document.changeOutput.target="mainFrame";
	document.changeOutput.submit();
	window.parent.topFrame.window.location.reload();
}

/************************************************************
变换排序规则 空,正序,逆序
参数: sel -- 传入触发事件的select对象:objselect
Author:王新强
************************************************************/
function changeOrderRule(sel){
	var i=sel.selectedIndex;
	var value=sel.options[i].value;
	var text=sel.options[i].innerHTML;
	vstate=value.split("#");
	tstate=text.split("[");
	if(vstate[1]==null){
	sel.options[i].value=vstate[0]+"#asc";
	sel.options[i].innerHTML=tstate[0]+"[正序]";
	}
	if(vstate[1]=="asc"){
	sel.options[i].value=vstate[0]+"#desc";
	sel.options[i].innerHTML=tstate[0]+"[逆序]";
	}
	if(vstate[1]=="desc"){
	sel.options[i].value=vstate[0];
	sel.options[i].innerHTML=tstate[0];
	}
}

/************************************************************
搜索结果字段原子的option加入到选择列表
Author:王新强
************************************************************/
function searchField(){
	document.all.SrcSelect.length=0;
	var key=document.getElementById("field").value;
	if(key){
		var count=0;
		for(i=0;i<atoms.length;i++){
			data=atoms[i];
			for(j=0;j<atoms[i].length;j++){
				result=atoms[i][j].text.indexOf(key)
				if(result>-1){
					document.getElementById("notfind").innerHTML="完成";
					document.all.SrcSelect.add(new Option(atoms[i][j].text,atoms[i][j].value));
					count++;
				}
			}
		}
		if(count==0){
			document.getElementById("notfind").innerHTML="未找到";
		}
	}
}
</script>
	</head>
	<body>
		<br>
		<br>
		<div
			style="border: 1px orange solid; height: 40px; width: 780px; float: left;">
			<form name="changeOutput" action="changeOutput.do">
				<table border="0" width="700">
					<tr>
						<td colspan="2" align="left">
							搜索字段:
							<input type="text" name="field" onpropertychange="searchField()"
								size="27">
						</td>
						<td colspan="2">
							<input id="copycondition" type="checkbox" name="copycondition"
								value="yes"
								<%
															String copycondition=(String)session.getAttribute("copycondition");
														if("yes".equals(copycondition)){out.print("checked");} %>
								onclick="querycondition()">
							复制条件&nbsp;&nbsp;
						</td>
					</tr>
					<tr>
						<td width="100%" colspan="2">
							字段类别:
							<select name="groups" id="groups" onchange="ChangeGroup()">
								<%
											Connection con = GetConnect.getCsConnection();
											String oldid = (String) session.getAttribute("oldid");
											String kindId = (String) session.getAttribute("kindId");
											SEG_BIZ_GROUP_DEFDAO dao = new SEG_BIZ_GROUP_DEFDAO(con);
											List grouplist = dao.findAllByKind(kindId);
											int j = 0;
											for (int i = 0; i < grouplist.size(); i++) {
												SEG_BIZ_GROUP_DEFDTO groupdto = (SEG_BIZ_GROUP_DEFDTO) grouplist
												.get(i);
												if (groupdto.getATOM_GROUP_ID() == 400006) {
													continue;
												}
												SEG_BOOKMARK_DEFDAO seg_bookmark_defdao = new SEG_BOOKMARK_DEFDAO(
												con);
												SEG_BOOKMARK_DEFDTO seg_bookmark_defdto = seg_bookmark_defdao
												.findByBId(groupdto.getBOOKMARK_ID());
												String name = seg_bookmark_defdto.getBOOKMARK_NAME();
								%>
								<option value=<%=groupdto.getATOM_GROUP_ID()%>><%=name + "→" + groupdto.getATOM_GROUP_NAME()%></option>
								<script type="text/javascript">atoms[<%=j%>]=new Array(); </script>
								<%
												SEG_ATOM_GROUP_RELDAO reldao = new SEG_ATOM_GROUP_RELDAO(con);
												List rellist = reldao.findById(groupdto.getATOM_GROUP_ID());
												List atomlist = new ArrayList();
												for (int k = 0; k < rellist.size(); k++) {
													SEG_ATOM_GROUP_RELDTO seg_ATOM_GROUP_RELDTO = (SEG_ATOM_GROUP_RELDTO) rellist
													.get(k);
													SEG_BIZ_ATOM_DEFDAO atomdao = new SEG_BIZ_ATOM_DEFDAO(
													con);
													SEG_BIZ_ATOM_DEFDTO atomdto = atomdao
													.findById(seg_ATOM_GROUP_RELDTO.getBIZ_ATOM_ID());
													if (atomdto.getVALUE_TYPE_ID() != 1000
													&& atomdto.getVALUE_TYPE_ID() != 2000) {
												atomlist.add(atomdto);
													}
												}
												if (atomlist.size() > 0)
													for (int m = 0; m < atomlist.size(); m++) {
												SEG_BIZ_ATOM_DEFDTO showdto = (SEG_BIZ_ATOM_DEFDTO) atomlist
														.get(m);
								%>
								<script type="text/javascript">atoms[<%=j%>][<%=m%>]=new Atomoption('<%=showdto.getBIZ_ATOM_NAME()%>',<%=showdto.getBIZ_ATOM_ID()%>) </script>
								<%
												}
												j++;
											}
								%>
							</select>
						</td>
						<td colspan="3">
							选择模板:&nbsp;
							<select id="templateId" name="templateId" onchange="querymodel()">
								<option value="0">
									无模板
								</option>
								<%
											IDA_TEMPLATEDAO templatedao = new IDA_TEMPLATEDAO(con);
											List templist = templatedao.findAll("20", "admin");
											Iterator it = templist.iterator();
											while (it.hasNext()) {
												IDA_TEMPLATEDTO templatedto = (IDA_TEMPLATEDTO) (it.next());
												String templatename = templatedto.getTemplateName();
												String templateid = templatedto.getTemplateId().toString();
								%>
								<option value="<%=templateid%>"
									<%
															String selectedtemp=request.getParameter("selectedtemplate");
															if(selectedtemp==null)
																selectedtemp=(String)request.getSession().getAttribute("selectedtemplate");
															if(templateid.equals(selectedtemp)){
													 			out.print(" selected");
															}
													 %>>
									<%=templatename%>
								</option>
								<%
								}
								%>
							</select>

						</td>
					</tr>
					<tr>
						<td colspan="2">
							可选结果字段:(可多选)
							<span id="notfind" style="width: 60px; color: red"></span>
						</td>
						<td colspan="3">
							已选结果字段:(选中双击可改变排序规则)
						</td>
					</tr>
					<tr>
						<td width="35%">
							<select name="SrcSelect" size=20
								style="font-size: 10pt; width: 250px;" multiple>
							</select>
						</td>
						<td width="10%" align="center">

							<a
								href="javascript:moveLeftOrRight(document.all.SrcSelect,document.all.ObjSelect)"><img
									src="images/toright.jpg" border="0"> </a>
							<br>
							<br>
							<a
								href="javascript:moveLeftOrRight(document.all.ObjSelect,document.all.SrcSelect)"><img
									src="images/toleft.jpg" border="0"> </a>
						</td>
						<td width="35%">
							<select name="ObjSelect" size=20
								style="font-size: 10pt; width: 250px;" multiple
								ondblclick="changeOrderRule(this)">
								<%
													List output = (List) request.getSession()
													.getAttribute("Outputlist");
											if (null != output) {
												Iterator its = output.iterator();
												while (its.hasNext()) {
													SEG_BIZ_ATOM_DEFDTO dto = (SEG_BIZ_ATOM_DEFDTO) its
													.next();
													String order = null;
													String tOrderRule = "";
													String vOrderRule = dto.getORDER_RULE();
													if (!(null == vOrderRule || "".equals(vOrderRule))) {
												if (dto.getORDER_RULE().equals("asc")) {
													order = "正序";
												} else {
													order = "逆序";
												}
												tOrderRule = "[" + order + "]";
												vOrderRule = "#" + vOrderRule;
													} else {
												vOrderRule = "";
													}
								%>
								<option value="<%=dto.getBIZ_ATOM_ID() + vOrderRule%>"><%=dto.getBIZ_ATOM_NAME() + tOrderRule%></option>
								<%
									}
									}
								%>
							</select>
						</td>

						<td width="10%" align="center">
							<a href="javascript:moveUp()"><img src="images/up.jpg"
									border="0" title="向上移动顺序"> </a>
							<br>
							<br>
							<a href="javascript:moveDown()"><img src="images/down.jpg"
									border="0" title="向下移动顺序"> </a>
						</td>
						<td width="10%" align="center" valign="bottom">
							<a href="javascript:submitchange()"><img
									src="images/send.jpg" border="0" title="提交查询"> </a>
						</td>
					</tr>
				</table>

				<input type="hidden" id="operation" name="method" value="">
			</form>
		</div>
		<script type="text/javascript">
					document.all.groups.selectedIndex=0;
					ChangeGroup();
					window.parent.topFrame.tips.innerHTML="<b>定制结果</b> <br> 选择一个查询结果模板，以确定查询结果显示的字段，结果字段可加以调整。选择\"复制条件\"，则细分条件字段默认出现在查询结果中。";
					
		</script>
	</body>
</html>